package com.project.XXXX.identification;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.project.XXXX.model.tbl_employeeinfo;
import com.project.XXXX.model.tbl_employerinfo;

public class IndentifyJspFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
          HttpServletRequest req=(HttpServletRequest)request;
          HttpServletResponse resp=(HttpServletResponse)response;
      	HttpSession session = req.getSession();  
   
          if(StringUtils.endsWithAny(req.getRequestURL(), "/Register.jsp","/Index.jsp","/Login.jsp","/Job.jsp","/PtJobdetail.jsp","/Citylist.jsp","/"))
          {
        		 chain.doFilter(request, response);
          }
          else
          {
        	 if (session.getAttribute("User") == null||session.getAttribute("EEinfo")==null||session.getAttribute("ERinfo")==null)
        			{
        				resp.sendRedirect("/Login.jsp");
        			}
        	else if(StringUtils.endsWith(req.getRequestURL(), "Job-Posted.jsp")&&!canPost((tbl_employerinfo)session.getAttribute("ERinfo"))){
        		
        		resp.sendRedirect("/EmployerInfo.jsp?msg=finishinfo");
        	}
        			else 
        			{
        			 chain.doFilter(request, response);		
        			
        			}
          }
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	public boolean canPost(tbl_employerinfo info) {
		if (info != null) {
			if(StringUtils.isAnyEmpty(info.getName() ,info.getComment(),info.getContactor(),info.getTel()))
				return false;
			else
			return true;
		}
		return false;
	}
}
